package com.dundun.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dundun.annotation.AuthCheck;
import com.dundun.common.utils.Ajax;
import com.dundun.common.utils.DundunJwtUtil;
import com.dundun.domain.*;
import com.dundun.mapper.BrowseMapper;
import com.dundun.mapper.CollectMapper;
import com.dundun.mapper.JobRelationshipMapper;
import com.dundun.service.BrowseService;
import com.dundun.service.CollectService;
import com.dundun.service.JobRelationshipService;
import com.dundun.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户相关接口")
@RequestMapping("/")
@RestController
public class UserController {
    @Resource
    UserService userService;
    @Resource
    JobRelationshipService jobRelationshipService;
    @Resource
    CollectService collectService;
    @Resource
    BrowseService browseService;
    @ApiOperation(value = "获取用户个人信息")
    @GetMapping("/GetIdentifyInfo")
    public Ajax GetIdentifyInfo(int userId) {
        return Ajax.success().put("data", "the return data...");
    }

    @ApiOperation(value = "获取用户Token")
    @GetMapping("/GetToken")
    public Ajax GetToken() {
        return Ajax.success().put("data", "the return data...");
    }

    @ApiOperation(value = "微信登录")
    @PostMapping("/UserLogin")
    public Ajax UserLogin(String code, String UserNickname, String UserAvatar) {
        String appid = "wxfa10ebb6aa9522e1";
        String secret = "32cc41c3d7a6490ab50b777c5d871ca8";
        String url = "https://api.weixin.qq.com/sns/jscode2session" + "?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
        String response = HttpUtil.createGet(url).execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(response);
        String openid = (String) jsonObject.get("openid");
        String sessionKey = (String) jsonObject.get("session_key");
        User user = userService.getOne(new QueryWrapper<User>().eq("open_id", openid));
        if (user == null) {
            // 注册
            user = new User();
            user.setOpenId(openid);
            user.setPhone("");
            user.setNickName(UserNickname);
            user.setAvatar(UserAvatar);
            user.setUserDelivery(0);
            user.setUserReceived(0);
            if (!userService.save(user)) {
                return Ajax.error("注册失败,请重试");
            }
            user = userService.getOne(new QueryWrapper<User>().eq("open_id", openid));
        }
        return Ajax.success().put("sessionKey", sessionKey).put("userId", user.getUserId()).put("token", DundunJwtUtil.getJwt(user));
    }

    @ApiOperation(value = "微信注册")
    @GetMapping("/UserRegister")
    public Ajax UserRegister() {
        return Ajax.success().put("data", "the return data...");
    }

    @ApiOperation(value = "获取用户关联数据")
    @GetMapping("/GetCounts")
    @AuthCheck
    public Ajax GetCounts(int userId) {
        Long deliverCount = jobRelationshipService.count(
                new QueryWrapper<JobRelationship>().eq("user_id", userId)
        );
        Long collectCount = collectService.count(
                new QueryWrapper<Collect>().eq("user_id", userId)
        );
        Long browseCount = browseService.count(
                new QueryWrapper<Browse>().eq("user_id", userId)
        );
        Map params = new HashMap();
        params.put("deliverCount", deliverCount);
        params.put("collectCount", collectCount);
        params.put("browseCount", browseCount);
        return Ajax.success().put("data", params);
    }
}
